/*
 * TabPanel.fx
 *
 * Created on 2010-12-28, 13:51:18
 */

package pl.elka.gis.main.view;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.effect.DropShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Stack;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.ShapeSubtract;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;

/**
 * @author looser
 */

public class TabPanel extends CustomNode {
  public-init var panelW:Float;
  public-init var panelH:Float;
  public-init var index:Integer;
  public-init var title:String;
  public var parentTabPane:Stack;
  public var content:Node[] = [];
  // The clickable tab area
  def tabRect = Rectangle {
        x: 10 + ((4 + 150) * index), y: 10
        arcHeight:7
        arcWidth:7
        width: 150, height: 25
        opacity:0
        onMousePressed: function( e: MouseEvent ):Void {
          var tabs:Node[] = parentTabPane.content;
          for (tab in tabs) {
            var tabPanel:TabPanel = tab as TabPanel;
            if (index == tabPanel.index) {
              delete tab from parentTabPane.content;
              insert tab into parentTabPane.content;
              break;
            } // found so make it the top of stack [add to end]
          } // loop through all tab panels
        }        
  }

  // tab text
  def tabText = Text {
    content: title
    textOrigin: TextOrigin.TOP
    font: Font.font("Verdana", FontWeight.BOLD, 10);
    x: 15 + ((4 + 150) * index)
    y: 15
  }

  // merge or blend the bottom of tab with the main content region
  def blendBottomOfTab = Rectangle {
    x: 10 + ((4 + 150) * index), y: 10 + 20
    width: 150, height: 25
  }

  // content area background area
  def tabPanelContentArea = Rectangle {
    x: 10, y: 10 + 25
    arcHeight:15
    arcWidth:15
    width: panelW, height: panelH
  }

  def tabArea = ShapeSubtract {
    a: [  tabRect,
          blendBottomOfTab,
          tabPanelContentArea
    ] // union tab and content area

    fill: LinearGradient {
            startX: 0.0, startY: 0.0, endX: 1.0, endY: 1.0
            proportional: true
            stops: [
                Stop { offset: 0.0 color: Color.WHITE }
                Stop { offset: 1.0 color: Color.GRAY }
            ]
    } // fill
    stroke: Color.BLACK
    effect: DropShadow {offsetX: 2 offsetY: 4}
  }

  public override function create(): Node {
    return Group {
      content: [tabArea, tabRect, content, tabText]
    }
  }
}

